<template>
  <list-detail-layout>
    <template #detailMain>
      <strategy-detail-main :ecuVersions="ecuVersions" :loadingData="loadingEcuVersions"></strategy-detail-main>
    </template>
    <template #detailRight>
      <strategy-detail-base-info
        :detailBaseInfoData="detailBaseInfoData"
        :loading-data="spinningLoading"
      >
      </strategy-detail-base-info>
      <!-- 统计信息 -->
      <strategy-detail-statistic class="strategyStatistic" :strategy-id="id" :time="time"></strategy-detail-statistic>
      <!-- 查看策略审核 -->
      <div class="strategyAuditProcess" v-if="versionType=='PROD'">
        <h4 class="withVerticleLineT4 title14">{{ $t('strategyAudit') }}</h4>
        <a @click="viewyAuditProcess">{{ $t('view') }}</a>
      </div>
      <audit-process-dialog ref="auditProcessDialogRef" :title="$t('strategyAudit')" :query="auditProcessQuery">
      </audit-process-dialog>
    </template>
  </list-detail-layout>
</template>
<script>
import ListDetailLayout from '@/views/components/ListDetailLayout'
import StrategyDetailMain from './StrategyDetailMain.vue'
import StrategyDetailBaseInfo from './StrategyDetailBaseInfo.vue'
import StrategyDetailStatistic from './StrategyDetailStatistic.vue'
import AuditProcessDialog from '@/views/components/AuditFlowProcess/dialog.vue'
export default {
  components: {
    StrategyDetailMain,
    StrategyDetailBaseInfo,
    ListDetailLayout,
    StrategyDetailStatistic,
    AuditProcessDialog
  },
  props: {
    versionType: {
      type: String,
      default: ''
    },
    upgradeTriggerType: {
      type: String,
      default: ''
    },
    id: {
      type: String,
      default: ''
    },
    isCopy: {
      type: Boolean,
      default: false
    },
    time: {
      type: Number,
      default: 784502
    },
    isUpdate: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    detailBaseInfoData () {
      if (!this.editRecord) {
        return {}
      }
      // eslint-disable-next-line max-len
      const keys = ['name', 'conditionStrategyName', 'createBy', 'createTime', 'vehicleVersionName', 'modelName', 'trimLevel']
      const info = {}
      keys.forEach(item => {
        info[item] = this.editRecord[item]
      })
      return info
    }
  },
  watch: {
    time: {
      handler () {
        this.getStrateDetail()
      },
      immediate: true
    }
  },
  data () {
    return {
      spinningLoading: false,
      loadingEcuVersions: false,
      editRecord: null,
      ecuVersions: [],
      linkedTaskIds: [],
      // 查看审核流程条件
      auditProcessQuery: {
        // 策略id
        businessKey: '',
        businessCode: 'strategy_audit',
        instanceId: ''
      }
    }
  },
  methods: {
    // 获取策略详情数据
    getStrateDetail () {
      console.log(this.versionType);
      this.ecuVersions = []
      this.editRecord = null
      if (!this.id) {
        return
      }
      this.spinningLoading = true
      this.$store.dispatch('getStrategyDetailData', {id : this.id}).then(res => {
        const data = res.data
        if (!data) {
          return
        }
        const obj = {
          id: this.id,
          name: data.name,
          createTime: data.createTime,
          createBy: data.createBy || '-',
          conditionStrategyName: data.conditionStrategyName || '-',
          modelName: data.modelName || '-',
          vehicleVersionName: data.vehicleVersionName || '-',
          trimLevel: data.trimLevel || '-'
        }
        this.editRecord = obj
        this.getEcuVersionsData(data.vehicleVersionId)
        if (data.instanceId) {
          this.auditProcessQuery.instanceId = data.instanceId
        }
      }).finally(() => {
        this.spinningLoading = false
      })
    },
    // 获取当前车辆版本的零件配置
    getEcuVersionsData (vehicleVersionId) {
      this.ecuVersions = []
      if (!vehicleVersionId) {
        return
      }
      this.loadingEcuVersions = true
      this.$store.dispatch('getVehicleVersionDetailData', { id: vehicleVersionId,versionType: this.$commonFun.getVersionTypeByTestType(this.versionType), isOnlyEcuVersion: true })
        .then(res => {
          console.log(res, 'ecuVersions')
          this.ecuVersions = res
        }).finally(() => {
          this.loadingEcuVersions = false
        })
    },
    // 查看审核过程
    viewyAuditProcess () {
      this.auditProcessQuery.businessKey = this.id
      if (this.auditProcessQuery.instanceId) {
        this.$refs.auditProcessDialogRef.toggleVisible(true)
      } else {
        this.$message.warning(this.$t('auditNotStarted'))
      }
    }
  }
}
</script>
<style scoped lang="less">
.strategyStatistic {
  margin-top: 20px;
}

.strategyAuditProcess {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
}
</style>
